/**
 * @file hash_utils.h
 *
 * Copyright (c) Huawei Technologies Co., Ltd. 2019-2020. All rights reserved.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 */

#ifndef ACL_UTILS_HASH_UTILS_H
#define ACL_UTILS_HASH_UTILS_H
#include "acl/acl_base.h"
#include "utils/string_utils.h"
#include "utils/attr_utils.h"
#include "types/op_attr.h"
#include "types/acl_op.h"
#include "types/tensor_desc_internal.h"
#include "common/log_inner.h"

namespace acl {
namespace hash_utils {

template <class T>
inline void HashCombine(size_t &seed, const T& v)
{
    const std::hash<T> hasher;
    seed ^= hasher(v) + 0x9E3779B9U + (seed << 6U) + (seed >> 2U);
}

inline void HashCombine(size_t &seed, const ge::DataType &v)
{
    const std::hash<int32_t> hasher;
    seed ^= hasher(static_cast<int32_t>(v)) + 0x9E3779B9U + (seed << 6U) + (seed >> 2U);
}

aclError GetTensorDescHash(const int32_t num, const aclTensorDesc *const descArr[], size_t &seed);

aclError GetAclOpHash(const AclOp &aclOp, const aclopAttr *const opAttr, const size_t attrDigest, size_t &seed);

aclError GetTensorDescHashDynamic(const int32_t num, const aclTensorDesc *const descArr[], size_t &seed);

aclError GetAclOpHashDynamic(const AclOp &aclOp, const aclopAttr *const opAttr,
                             const size_t attrDigest, size_t &seed, const uint64_t seq);

template<typename T>
bool CheckModelAndAttrMatch(const AclOp &aclOp, const aclopAttr *const opAttr, const T &entry)
{
    if (entry == nullptr) {
        return false;
    }
    if (aclOp.opType != entry->opType) {
        return false;
    }

    if (aclOp.numInputs != static_cast<int32_t>(entry->inputDescArr.size())) {
        return false;
    }

    for (size_t i = 0U; i < static_cast<size_t>(aclOp.numInputs); ++i) {
        if (!(entry->inputDescArr[i] == aclOp.inputDesc[i])) {
            return false;
        }
    }

    if (aclOp.numOutputs != static_cast<int32_t>(entry->outputDescArr.size())) {
        return false;
    }

    for (size_t i = 0U; i < static_cast<size_t>(aclOp.numOutputs); ++i) {
        if (!(entry->outputDescArr[i] == aclOp.outputDesc[i])) {
            return false;
        }
    }

    if (!attr_utils::OpAttrEquals(opAttr, &(entry->opAttr))) {
        return false;
    }
    return true;
}

template<typename T>
bool CheckModelAndAttrMatchDynamic(const AclOp &aclOp, const aclopAttr *const opAttr, const T &entry,
    const uint64_t seq)
{
    if (entry == nullptr) {
        return false;
    }
    if (aclOp.opType != entry->opType) {
        return false;
    }

    if (seq != entry->seq) {
        return false;
    }

    if (aclOp.numInputs != static_cast<int32_t>(entry->inputDescArr.size())) {
        return false;
    }

    for (size_t i = 0U; i < static_cast<size_t>(aclOp.numInputs); ++i) {
        if (!(entry->inputDescArr[i].IsSameTensor(aclOp.inputDesc[i]))) {
            return false;
        }
    }

    if (aclOp.numOutputs != static_cast<int32_t>(entry->outputDescArr.size())) {
        return false;
    }

    for (size_t i = 0U; i < static_cast<size_t>(aclOp.numOutputs); ++i) {
        if (!(entry->outputDescArr[i].IsSameTensor(aclOp.outputDesc[i]))) {
            return false;
        }
    }

    if (!attr_utils::OpAttrEquals(opAttr, &(entry->opAttr))) {
        return false;
    }
    return true;
}

} // namespace hash_utils
} // namespace acl

#endif // ACL_UTILS_HASH_UTILS_H
